﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CrossGenGUI
{
    public partial class Edit : Form
    {
        private string CurrentPath;

        public Edit()
        {
            InitializeComponent();
        }

        private void LoadData(string path)
        {
            try
            {
                CurrentPath = path;
                StreamReader reader = new StreamReader(path, System.Text.Encoding.Default, false);
                textBox1.Text = reader.ReadToEnd();
                reader.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show("Произошла ошибка при чтении из словаря. " + e.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            switch (listBox1.SelectedIndex)
            {
                case 0:
                    LoadData("Dictionaries\\General.cdic");
                    break;
                case 1:
                    LoadData("Dictionaries\\tech.cdic");
                    break;
                case 2:
                    LoadData("Dictionaries\\history.cdic");
                    break;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                FileStream fs = new FileStream(CurrentPath, FileMode.Create, FileAccess.Write);
                fs.SetLength(0);
                StreamWriter streamwriter = new StreamWriter(fs, System.Text.Encoding.Default);
                streamwriter.Write(textBox1.Text);
                streamwriter.Flush();
                streamwriter.Close();
                fs.Close();
            }
            catch(Exception exp)
            {
                MessageBox.Show("Произошла ошибка при записи в словарь. " + exp.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
